package development.game;

import java.util.Random;

import state.DrawListener;
import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.Sprite;
import state.TimerListener;
import state.font.FontManager;
import state.font.ImageText;
import state.media.AudioManager;
import state.media.Player;
import state.util.GameColor;
import state.util.GameStringUtils;
import state.util.Hashtable;
import state.util.ImageUtil;
import state.util.NumberUtil;
import state.util.array.StringArray;
import development.Constants;
import development.DrawUtils;
import development.FrmDownloadResource;
import development.Global;
import development.group.FrmGroupMenu;
import development.group.FrmGroupNapXuAndExchangeGold;
import development.listener.DownloadResourceListener;

public class FrmDaoVangOffline extends GameForm implements TimerListener {
  private Sprite groundSprite;
  private Sprite ngamSprite;
  private Sprite personSprite;
  private Sprite[] objectSprite;
  private GameImage[] personImage;
  private GameImage[] ngamImages;
  private GameImage ngamImage;
  private GameImage[] objectImages;
  private GameImage personLargeImg;
  private GameImage bgImage;
  private GameImage sucmanhImg;
  private int[] ObjX;
  private int[] ObjY;
  private int[] xLine;
  private int[] yLine;
  private float X;
  private int Y;
  private StringArray nameObject;
  private int step;
  private int indexToado;
  private int selectIndex;
  private int X2;
  private int Y2;
  private boolean isDrawLine1;
  private boolean isDrawLine2;
  private String[] randString1;
  private String[] randString2;
  private int _speed;
  private int countObject;
  private int _k;
  private int dx;
  private int scoreMin;
  private int time;
  private Random rd;
  private boolean sucmanh;
  private GameImage bosoImg;
  private GameImage[] bosoImage;
  private String scoreStr;
  private String scoreMinStr;
  private String timeStr;
  private String levelStr;
  private int count;
  private GameImage luudanImg;
  private boolean ispause;
  private boolean isCancelObject;
  private int[] dxMouse;
  private boolean isTNT;
  private GameImage[] noImgs;
  private int Y0;// = 92;
  private int X0;// = 120;
  private GameImage bucdungImg;
  private boolean isdrawSucmanh;
  private int[] ext;
  private int[] extY;
  public float[] TRIANGLE;
  private float[] cosin;
  private int endLevelCase;
  private ImageText text8;
  private GameImage infoImage;
  private GameImage bottomImage;
  private GameImage endgameImage;
  private Sprite resultSprite;

  // //////////////////////
  public int volume = 50; // 0 ~ 100;
  public Player player;

  public static int level = 0;
  public static int maxObject = 20;
  public static int maxLevel;
  public static int score = 0;
  public static boolean f1 = false;
  public static boolean f2 = false;
  public static int f3 = 0;
  public static boolean f4 = false;
  public static boolean f5 = false;
  // public int volume = 50; // 0 ~ 100;
  // public Player player;
  public static int _i = 0;
  private int SCREEN_WIDTH = 240;
  private int SCREEN_HEIGH = 320;
  private static GameForm singleton;

  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmDaoVangOffline();
    }
    return singleton;
  }
  
  public void init(Hashtable inputParameters) {
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    ext = new int[]{-7, -6, -5, -4, -3, -3, -3, -2, -2, -2, -2, -1, -1, 0, 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 6, 7};
    extY = new int[]{-4, -4, -3, -3, -3, -3, -2, -2, -2, -2, -2, -1, -1, 0, 0, 0, -1, -1, -2, -2, -2, -2, -3, -3, -3, -3, -4, -4};
    TRIANGLE = new float[]{3.732050807568877f, 2.747477419454622f, 2.144506920509559f, 1.732050807568877f, 1.428148006742114f, 1.19175359259421f, 1.0f, 0.83909963117728f, 0.7002075382097098f, 0.5773502691896257f, 0.4663076581549986f,
        0.3639702342662023f, 0.267949192431123f, 0.176326980708465f};
    cosin = new float[]{0.2588190451025207f, 0.3420201433256687f, 0.4226182617406994f, 0.5f, 0.5735764363510461f, 0.6427876096865394f, 0.7071067811865476f, 0.766044443118978f, 0.819152044288992f, 0.866025403784439f, 0.9063077870366499f,
        0.9396926207859084f, 0.9659258262890683f, 0.984807753012208f};
    isDrawLine2 = false;
    sucmanh = false;
    ispause = false;
    endLevelCase = 0;
    isCancelObject = false;
    count = 0;
    indexToado = 0;
    step = 0;
    _speed = 0;
    _k = 0;
    dx = 1;
    time = 45;
    
    if (Constants.IS_240x320_SCREEN) {
      SCREEN_WIDTH = 240;
      SCREEN_HEIGH = 320;
    } else {
      SCREEN_WIDTH = 320;
      SCREEN_HEIGH = 240;
    }
    
    X0 = SCREEN_WIDTH / 2;
    if (Constants.IS_240x320_SCREEN) {
      Y0 = 92;
      xLine = new int[]{104, 105, 106, 106, 108, 109, 110, 111, 112, 113, 115, 117, 118, 119, 121, 123, 124, 125, 127, 128, 129, 130, 131, 132, 133, 133, 134};
    } else {
      Y0 = 52;
      xLine = new int[]{144, 145, 146, 146, 148, 149, 150, 151, 152, 153, 155, 157, 158, 159, 161, 163, 164, 165, 167, 168, 169, 170, 171, 172, 173, 173, 174};
    }
    randString1 = new String[]{"75", "155", "256", "310", "430", "535", "600", "815", "sucmanh", "luudan"};
    randString2 = new String[]{"25", "105", "206", "270", "350", "435", "520", "715", "sucmanh", "luudan"};

    if (Constants.IS_240x320_SCREEN) {
      yLine = new int[]{95, 98, 100, 102, 104, 105, 106, 107, 108, 109, 110, 110, 110, 110, 110, 110, 109, 108, 107, 106, 105, 104, 102, 100, 98, 97, 95};
    } else {
      yLine = new int[]{55, 58, 60, 62, 64, 65, 66, 67, 68, 69, 70, 70, 70, 70, 70, 70, 69, 68, 67, 66, 65, 64, 62, 60, 58, 57, 55};
    }
    rd = new Random();
    countObject = getCountObject("/DaoVang/level" + (level + 1) + ".txt");
    endgameImage = ImageUtil.getImage(Constants.DAO_VANG_IMAGE_CATEGORY, "end.png");
    ObjX = new int[countObject];
    ObjY = new int[countObject];
    bosoImage = new GameImage[11];
    ngamImages = new GameImage[27];
    ngamImage = ImageUtil.getImage(Constants.DAO_VANG_IMAGE_CATEGORY, "ngam.png");
    for (int i = 0; i < 27; i++) {
      ngamImages[i] = ImageUtil.rotate(ngamImage, 360 - 5 * i);
    }
    bucdungImg = ImageUtil.getImage(Constants.DAO_VANG_IMAGE_CATEGORY, "bucdung.png");
    GameImage noImg = ImageUtil.getImage(Constants.DAO_VANG_IMAGE_CATEGORY, "no.png");
    noImgs = new GameImage[7];
    for (int i = 0; i < 7; i++) {
      noImgs[i] = ImageUtil.getSubImage(noImg, i * 41, 0, 41, 41, true);
    }
    noImg = null;
    bosoImg = ImageUtil.getImage(Constants.DAO_VANG_IMAGE_CATEGORY, "boso.png");
    for (int i = 0; i < 11; i++) {
      bosoImage[i] = ImageUtil.getSubImage(bosoImg, 9 * i, 0, 8, 12, true);
    }
    bosoImg = null;
    if (Constants.IS_240x320_SCREEN) {
      bgImage = ImageUtil.getImage(Constants.DAO_VANG_IMAGE_CATEGORY, "bg_play.png");
      bottomImage = ImageUtil.getSubImage(bgImage, 56, 296, 2, 24, true);
      bottomImage = ImageUtil.joinAndCreateImages(bottomImage, SCREEN_WIDTH, 24);
    } else {
      bgImage = ImageUtil.getImage(Constants.DAO_VANG_IMAGE_CATEGORY, "bg_play_320.png");
      bottomImage = ImageUtil.getSubImage(bgImage, 76, 214, 2, 26, true);
      bottomImage = ImageUtil.joinAndCreateImages(bottomImage, SCREEN_WIDTH, 26);
    }
    personImage = new GameImage[5];
    personLargeImg = ImageUtil.getImage(Constants.DAO_VANG_IMAGE_CATEGORY, "person.png");
    for (int i = 0; i < 5; i++) {
      personImage[i] = ImageUtil.getSubImage(personLargeImg, i * 44, 0, 44, 44, true);
    }
    personLargeImg = null;
    nameObject = readMapText("/DaoVang/level" + (level + 1) + ".txt", ObjX, ObjY);
    objectImages = new GameImage[nameObject.len()];
    for (int i = 0; i < nameObject.len(); i++) {
      objectImages[i] = ImageUtil.getImage(Constants.DAO_VANG_IMAGE_CATEGORY, nameObject.array[i] + ".png");
    }
    luudanImg = ImageUtil.getImage(Constants.DAO_VANG_IMAGE_CATEGORY, "1.png");
    infoImage = ImageUtil.getImage(Constants.DAO_VANG_IMAGE_CATEGORY, "diem1.png");
    dxMouse = new int[nameObject.len()];
    for (int i = 0; i < nameObject.len(); i++) {
      dxMouse[i] = -3;
    }

    objectSprite = new Sprite[countObject];
    groundSprite = new Sprite(null, _manager, 0, 0, Constants.TOP_LEFT_ANCHOR);
    groundSprite.setSize(SCREEN_WIDTH, SCREEN_HEIGH);
    if (Constants.IS_240x320_SCREEN) {
      personSprite = new Sprite(personImage[0], _manager, SCREEN_WIDTH / 2, Y0 + 13, GameConstants.BOTTOM_HCENTER_ANCHOR);
    } else {
      personSprite = new Sprite(personImage[0], _manager, SCREEN_WIDTH / 2, Y0 + 13, GameConstants.BOTTOM_HCENTER_ANCHOR);
    }
    
    ngamSprite = new Sprite(ngamImage, _manager, xLine[indexToado] + ext[indexToado], yLine[indexToado] + extY[indexToado], GameConstants.TOP_HCENTER_ANCHOR);

    ngamSprite.setDrawListener(new DrawListener() {

      public void paint(Sprite source, Graphics g) {

        ngamSprite.setImage(ngamImages[indexToado]);
        ngamSprite.setAnchor(GameConstants.TOP_HCENTER_ANCHOR);
      }
    });
    groundSprite.setDrawListener(new DrawListener() {
      public void paint(Sprite source, Graphics g) {
        g.drawImage(bgImage, 0, 0, GameConstants.TOP_LEFT_ANCHOR);
        g.drawImage(bucdungImg, X0, Y0 + 18, GameConstants.BOTTOM_HCENTER_ANCHOR);
        g.setColor(GameColor.BLACK_CODE);
        if (isTNT) {
          for (int i = 0; i < nameObject.len(); i++) {
            if (Math.abs(objectSprite[i].getRealX() - objectSprite[_k].getRealX()) < 50 && Math.abs(objectSprite[i].getRealY() - objectSprite[_k].getRealY()) < 50) {
              objectSprite[i].setVisible(false);
            }
          }
          objectSprite[_k].setVisible(false);

          count = count + 1;
          if (count < 50) {
            if (count % 5 == 0) {
              g.drawImage(noImgs[count % 7], objectSprite[_k].getRealX(), objectSprite[_k].getRealY(), GameConstants.CENTER_ANCHOR);
            }
          } else {
            isTNT = false;
          }
        }
        if (sucmanh) {
          count++;
          if (isdrawSucmanh) {
            if (count / 10 < 4) {
              g.drawImage(sucmanhImg, SCREEN_WIDTH / 2 - 110, 80, GameConstants.TOP_LEFT_ANCHOR);
            } else {
              isdrawSucmanh = false;
            }
          }

        }
        if (!isDrawLine1 && !isDrawLine2) {
          g.drawLine(SCREEN_WIDTH / 2, Y0, xLine[indexToado], yLine[indexToado]);
        } else {
          g.drawLine(SCREEN_WIDTH / 2, Y0, (int) X, Y);
        }

        scoreStr = String.valueOf(FrmDaoVangOffline.score);
        scoreMinStr = String.valueOf(scoreMin);
        timeStr = String.valueOf(time);
        levelStr = String.valueOf(FrmDaoVangOffline.level + 1);
        g.drawImage(infoImage, 10, 3, GameConstants.TOP_LEFT_ANCHOR);
        for (int i = 0; i < scoreStr.length(); i++) {
          String c = String.valueOf(scoreStr.charAt(i));
          g.drawImage(bosoImage[NumberUtil.parseInt(c) + 1], 32 + i * 9, 6, GameConstants.TOP_LEFT_ANCHOR);
        }
        for (int j = 0; j < String.valueOf(scoreMinStr).length(); j++) {
          String c = String.valueOf(scoreMinStr.charAt(j));
          g.drawImage(bosoImage[NumberUtil.parseInt(c) + 1], 32 + j * 9, 19, GameConstants.TOP_LEFT_ANCHOR);
        }
        for (int j = 0; j < String.valueOf(timeStr).length(); j++) {
          String c = String.valueOf(timeStr.charAt(j));
          g.drawImage(bosoImage[NumberUtil.parseInt(c) + 1], SCREEN_WIDTH / 2 + 50 + j * 9, 25, GameConstants.TOP_LEFT_ANCHOR);
        }
        g.drawImage(bottomImage, 0, SCREEN_HEIGH, GameConstants.BOTTOM_LEFT_ANCHOR);
        text8.drawString2(g, "Level:", GameColor.BLACK_CODE, 32, 31, GameConstants.TOP_LEFT_ANCHOR);
        for (int j = 0; j < String.valueOf(levelStr).length(); j++) {
          String c = String.valueOf(levelStr.charAt(j));

          g.drawImage(bosoImage[NumberUtil.parseInt(c) + 1], 72 + j * 9, 35, GameConstants.TOP_LEFT_ANCHOR);
        }
        for (int i = 0; i < FrmDaoVangOffline.f3; i++) {
          g.drawImage(luudanImg, SCREEN_WIDTH / 2 + 45 + i * 10, Y0 + 20, GameConstants.BOTTOM_LEFT_ANCHOR);
        }
       /* if (isNotEnoughMoney) {
    		g.setColor(Color.BLACK_CODE);
    		g.fillRect(SCREEN_WIDTH/2 - 100, SCREEN_HEIGHT/2 - 50, 200, 20);
    		text8.drawString(g, "Bạn không đủ tiền để chơi game", Color.WHITE_CODE, SCREEN_WIDTH/2, SCREEN_HEIGHT/2 - 40, GameConstants.CENTER_ANCHOR);
    	}*/
      }
    });
    for (int i = 0; i < nameObject.len(); i++) {
      objectSprite[i] = new Sprite(objectImages[i], _manager, ObjX[i], ObjY[i], Constants.TOP_LEFT_ANCHOR);
    }
    resultSprite = new Sprite(null, _manager, 0, 0, Constants.TOP_LEFT_ANCHOR);
    resultSprite.setSize(SCREEN_WIDTH, SCREEN_HEIGH);
    resultSprite.setDrawListener(new DrawListener() {
      public void paint(Sprite source, Graphics g) {
        if (endLevelCase == 2) {
          g.drawImage(endgameImage, SCREEN_WIDTH / 2, SCREEN_HEIGH / 2, GameConstants.CENTER_ANCHOR);
          DrawUtils.setBottomBar(_manager, "", "Chơi lại", "Thoát");
        }
      }
    });
    setTimerListener(this);
    GameGlobal.systemCanvas.setTimerDelay(80);
    DrawUtils.setBottomBar(_manager, "Tạm dừng", "", "Thoát");
    _manager.enableTouchPad();
  }
  
  public void alertEventPerform(int alertType, int eventType, int alertId) {
	    // Khi người chơi nạp điểm
	    if (alertId == 99) {
	      if (eventType == 0) {
	        FrmGroupNapXuAndExchangeGold.backForm = FrmGroupMenu.instance();
	        Global.nextState(FrmGroupNapXuAndExchangeGold.instance(), null);
	      } else if (eventType == 1) {
	        FrmGroupNapXuAndExchangeGold.backForm = FrmGroupMenu.instance();
	        Global.nextState(FrmGroupNapXuAndExchangeGold.instance(), null);
	        FrmGroupNapXuAndExchangeGold.setTab(1);
	      } else {
	        FrmGroupMenu.tabIndex = 1;
	    	  Global.nextState(FrmGroupMenu.instance(), null);
	      }
	    }
	  }
  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.FIRE:
      if (endLevelCase == 2) {
        time = 0;
        DownloadResourceListener downloadResourceListener = new DownloadResourceListener(Constants.DAO_VANG_IMAGE_CATEGORY);
        downloadResourceListener.nextForm = FrmDaoVangOffline.instance();
        Global.authenticateHandle.addListener(downloadResourceListener);
        GameGlobal.nextState(FrmDownloadResource.instance(), null);
        FrmDaoVangOffline.score = 0;
        FrmDaoVangOffline.level = 0;
      }
      break;
    case Key.DOWN:

      // if (!isEndgame) {
      if (!isDrawLine2 && !isDrawLine1) {
        isDrawLine1 = true;
        selectIndex = indexToado;
        X2 = xLine[selectIndex];
        Y2 = yLine[selectIndex];
        X = X2;
        Y = Y2;
      }
      break;
    case Key.SOFT_RIGHT:
    	  level = 0;
    	  f1 = false;
    	  f2 = false;
    	  f3 = 0;
    	  f4 = false;
    	  f5 = false;
    	score = 0;
      GameGlobal.nextState(FrmGroupMenu.instance(), null);
      break;
		case Key.SOFT_LEFT:
			ispause = !ispause;
			if (ispause) {
				DrawUtils.getBottomBar(_manager).setLeftLabel("Chơi tiếp");
			} else {
				DrawUtils.getBottomBar(_manager).setLeftLabel("Tạm dừng");
			}
			break;
		case Key.UP:
			if (FrmDaoVangOffline.f3 > 0) {
				if (isDrawLine2) {
					FrmDaoVangOffline.f3--;
					isCancelObject = true;
				}
			}
			break;
    default:
      break;
    }
  }

  public static void saveLevelInfo() {
  }

  public int processAfterCollides() {
    int speed = 0;
    for (int i = 0; i < countObject; i++) {
      if (ngamSprite.checkCollides1(objectSprite[i])) {
        _k = i;
        isDrawLine1 = false;
        isDrawLine2 = true;
        if (nameObject.array[i].equals("tnt")) {
          isTNT = true;
          isDrawLine2 = false;
          processAfterPull();
        } else {
          if (nameObject.array[i].compareTo("vangto") == 0 || nameObject.array[i].compareTo("dato") == 0) {
            speed = 2;
          } else if (nameObject.array[i].compareTo("vangvua") == 0 || nameObject.array[i].compareTo("da") == 0) {
            speed = 4;
          } else if (nameObject.array[i].compareTo("vangnho") == 0 || nameObject.array[i].compareTo("goiqua") == 0 || nameObject.array[i].compareTo("kimcuong") == 0 || nameObject.array[_k].compareTo("chuotKC") == 0 || nameObject.array[i].compareTo("chuot") == 0) {
            speed = 8;
          } else if (nameObject.array[i].compareTo("xuong") == 0) {
            speed = 8;
          } else {
            speed = 8;
          }
        }
      }
    }
    if (sucmanh || FrmDaoVangOffline.f1) {
      if (speed > 1) {
        speed = 8;
      }

    }
    return speed;
  }

  public int costObject(int k) {
    int cost = 0;
    if (nameObject.array[k].compareTo("vangto") == 0) {
      cost = 500;
    }
    if (nameObject.array[k].compareTo("vangvua") == 0) {
      cost = 200;
    }
    if (nameObject.array[k].compareTo("vangnho") == 0) {
      cost = 100;
    }
    if (nameObject.array[k].compareTo("dato") == 0) {
      if (FrmDaoVangOffline.f2) {
        cost = 50;
      } else {
        cost = 25;
      }
    }
    if (nameObject.array[k].compareTo("da") == 0) {
      if (FrmDaoVangOffline.f2) {
        cost = 30;
      } else {
        cost = 15;
      }
    }
    if (nameObject.array[k].compareTo("kimcuong") == 0) {
      if (FrmDaoVangOffline.f4) {
        cost = 800;
      } else {
        cost = 600;
      }
    }
    if (nameObject.array[k].compareTo("xuong") == 0) {
      cost = 2;
    }
    if (nameObject.array[k].compareTo("daulau") == 0) {
      cost = 5;
    }
    if (nameObject.array[k].compareTo("chuot") == 0) {
      cost = 4;
    }
    if (nameObject.array[k].compareTo("chuotKC") == 0) {
      if (FrmDaoVangOffline.f4) {
        cost = 800;
      } else {
        cost = 600;
      }
    }
    if (nameObject.array[k].compareTo("goiqua") == 0) {
      int tmp = rd.nextInt(10);
      if (FrmDaoVangOffline.f5) {
        if (tmp <= 7) {
          cost = NumberUtil.parseInt(randString1[tmp]);
        } else if (tmp == 8) {
          sucmanh = true;
        } else {
          FrmDaoVangOffline.f3 += 1;
        }
      } else {
        if (tmp <= 7) {
          cost = NumberUtil.parseInt(randString2[tmp]);
        } else if (tmp == 8) {
          sucmanh = true;
        } else {
          FrmDaoVangOffline.f3 += 1;
        }
      }
    }
    return cost;
  }

  // public void tinhDiem(int k) {
  // curentscore += costObject(k);
  // }

  public void processAfterPull() {
    // tinhDiem(k);
    FrmDaoVangOffline.score += costObject(_k);
    isDrawLine2 = false;
    count = 0;
    objectSprite[_k].setVisible(false);
    ngamSprite.setPosition(xLine[indexToado] + ext[indexToado], yLine[indexToado] + extY[indexToado]);
  }

  public void doTask() {
    if (!ispause) {
      step = (step + 1) % 300;
      if (checkChuot()) {
        xuLiDoituongChuot();
      }
      if (step % 20 == 0 && time > 0) {
        time -= 1;
      }
      if (time <= 0) {
        isDrawLine1 = false;
        isDrawLine2 = false;
        if (checkPassLevel()) {
          endLevelCase = 1;
          GameGlobal.systemCanvas.nextState(FrmCuahang.instance(), null);
          return;
        } else {
          endLevelCase = 2;
          return;
        }
      } else {
        if (isDrawLine1) {
          if (X < 0 || X > SCREEN_WIDTH || Y > SCREEN_HEIGH - 30) {
            isDrawLine1 = false;
          }

          if (selectIndex == 14) {
            Y += 4;
          } else if (selectIndex < 14) {
            Y += 4 * cosin[selectIndex];
            X = (X0 - (Y - Y0) * this.TRIANGLE[selectIndex]);
          } else {
            Y += 4 * cosin[(27 - selectIndex)];
            X = (X0 + (Y - Y0) * this.TRIANGLE[(27 - selectIndex)]);
          }

          ngamSprite.setPosition((int) X + ext[indexToado], Y + extY[indexToado]);
          _speed = processAfterCollides();
        }

        if (isDrawLine2) {
          if (step % (8 / _speed) == 0) {
            if (selectIndex == 14) {
              Y -= _speed;
            } else if (selectIndex < 14) {
              Y -= _speed * cosin[selectIndex];
              X = (X0 - (Y - Y0) * TRIANGLE[selectIndex]);
            } else {
              Y -= _speed * cosin[27 - selectIndex];
              X = (X0 + (Y - Y0) * TRIANGLE[(27 - selectIndex)]);
            }
          }

          personSprite.setImage(personImage[step % 5]);
          personSprite.setAnchor(GameConstants.BOTTOM_HCENTER_ANCHOR);
          // }
          ngamSprite.setPosition((int) X + ext[indexToado], Y + extY[indexToado]);
          objectSprite[_k].setPosition((int) (X - objectImages[_k].getWidth() / 2 + ext[indexToado]), Y + extY[indexToado]);
          if (ngamSprite.checkCollides1(personSprite)) {
            play("/sound/getobj.mid");
            processAfterPull();
          }
          if (isCancelObject) {
            processAfterPull();
            isCancelObject = false;
          }
        }

        if (!isDrawLine1 && !isDrawLine2) {
          // if (step % 2 == 0) {
          indexToado += dx;
          if (indexToado == 0 || indexToado == 26) {
            dx = -dx;
          }
          ngamSprite.setPosition(xLine[indexToado] + ext[indexToado], yLine[indexToado] + extY[indexToado] - 4);
        }
      }
    }
  }

  public boolean checkPassLevel() {
    boolean check = false;
    if (FrmDaoVangOffline.score >= scoreMin) {
      check = true;
    }
    return check;
  }

  public boolean checkChuot() {
    boolean check = false;
    for (int i = 0; i < nameObject.len(); i++) {
      if (nameObject.array[i].compareTo("chuot") == 0 || nameObject.array[i].compareTo("chuotKC") == 0) {
        check = true;
      }
    }
    return check;
  }

  public void xuLiDoituongChuot() {
    for (int i = 0; i < nameObject.len(); i++) {
      if (nameObject.array[i].compareTo("chuot") == 0 || nameObject.array[i].compareTo("chuotKC") == 0) {
        if (i != _k) {
          if (objectSprite[i].getRealX() >= SCREEN_WIDTH) {
            dxMouse[i] = dxMouse[i] * (-1);
            objectSprite[i].setImage(objectImages[i]);
            objectSprite[i].setAnchor(GameConstants.TOP_LEFT_ANCHOR);
          }
          if (objectSprite[i].getRealX() <= 0) {
            dxMouse[i] = dxMouse[i] * (-1);
            objectSprite[i].setImage(ImageUtil.transformMirror(objectImages[i]));
            objectSprite[i].setAnchor(GameConstants.TOP_LEFT_ANCHOR);
          }

          objectSprite[i].move(dxMouse[i], 0);
        }
      }
    }
  }

  public int getCountObject(String textFile) {
    String text = String.valueOf(DaoVangMap.DAO_VANG_MAP.get(textFile));
    StringArray lines = GameStringUtils.split(text, "\n");
    return NumberUtil.parseInt(lines.get(0));
  }

  public StringArray readMapText(String textFile, int[] x, int[] y) {
    String text = String.valueOf(DaoVangMap.DAO_VANG_MAP.get(textFile));
    StringArray lines = GameStringUtils.split(text, "\n");
    int numberOfResource = NumberUtil.parseInt(lines.get(0));
    String[] name = new String[numberOfResource];
    for (int i = 1; i < lines.len() - 1; i++) {
      StringArray tmp = GameStringUtils.split(lines.get(i), " ");
      name[i - 1] = tmp.get(0).trim();
      
      StringArray tmp1 = GameStringUtils.split(tmp.get(1), ",");
      x[i - 1] = NumberUtil.parseInt(tmp1.get(0).trim());
      y[i - 1] = NumberUtil.parseInt(tmp1.get(1).trim());
      if (!Constants.IS_240x320_SCREEN) {
        x[i - 1] += 40; // đồng hóa tọa độ bản dọc với bản ngang
        y[i - 1] -= 40;
      }
    }
    return new StringArray(name, numberOfResource);
  }

  public void play(String fileName) {
    if (player != null) {
      player.stop();
      player = null;
    }

    player = AudioManager.createPlayer(fileName, "audio/midi");
    player.prefetch();
    player.getControl("VolumeControl").setLevel(volume);
    player.start();
  }

  public void destroy() {
    groundSprite = null;
    ngamSprite = null;
    personSprite = null;
    objectSprite = null;
    personImage = null;
    ngamImage = null;
    objectImages = null;
    personLargeImg = null;
    bgImage = null;
    sucmanhImg = null;
    ObjX = null;
    ObjY = null;
    xLine = null;
    yLine = null;
    nameObject = null;
    randString1 = null;
    randString2 = null;
    rd = null;
    bosoImg = null;
    bosoImage = null;
    scoreStr = null;
    scoreMinStr = null;
    timeStr = null;
    levelStr = null;
    luudanImg = null;
    ngamImages = null;
    dxMouse = null;
    noImgs = null;
    bucdungImg = null;
    ext = null;
    extY = null;
    TRIANGLE = null;
    cosin = null;
    bottomImage = null;
    infoImage = null;
    bottomImage = null;
    endgameImage = null;
    resultSprite = null;
  }


}